﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BAL;
using Common;
using Entity;
using System.Text;
namespace Web
{
    public partial class Statistics : System.Web.UI.Page
    {
        protected IDictionary<int, string> StatusDic = new Dictionary<int, string>();
        protected IDictionary<int, string> SubjectDic = new Dictionary<int, string>();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!UserValidate.CheckLogin(base.Request, "Statistics"))
            {
                Response.Write("<script type=\"text/javascript\">top.location.href='login.aspx';</script>");
                Response.Redirect("login.aspx");
                Response.End();
            }
            LoadDic();
            if (!IsPostBack)
            {
                BindSubjectList();
                BindStatusList();
            }
            if (IsPostBack)
            {
                StatisticsCount();
            }
        }
        private void BindStatusList()
        {
            StatusList.Items.Clear();
            foreach (KeyValuePair<int, string> pair in StatusDic)
            {
                ListItem item = new ListItem();
                item.Text = pair.Value;
                item.Value = pair.Key.ToString();
                this.StatusList.Items.Add(item);
            }
        }

        private void BindSubjectList()
        {
            SubjectList.Items.Clear();
            foreach (KeyValuePair<int, string> pair in this.SubjectDic)
            {
                ListItem item = new ListItem();
                item.Text = pair.Value;
                item.Value = pair.Key.ToString();
                SubjectList.Items.Add(item);
            }
        }
        private string CreateBlock(string InputTitle, StatisticsInfo InputCount, string Make)
        {
            string str = "";
            StringBuilder builder = new StringBuilder();
            builder.Append("<h5>");
            builder.Append(InputTitle);
            builder.Append("</h5><div><label><a href='javascript:ShowDetail(0,");
            builder.Append(Make);
            builder.Append(")'>总计</a>：</label><h4>");
            builder.Append(InputCount.AllCount);
            builder.Append("</h4></div>");
            foreach (KeyValuePair<int, int> pair in InputCount.Counts)
            {
                str = string.Format("PR_{0}_{1}", Make, pair.Key);
                builder.Append("<div><label><a href='javascript:ShowDetail(");
                builder.Append(pair.Key);
                builder.Append(",");
                builder.Append(Make);
                builder.Append(");'>");
                builder.Append(this.StatusDic[pair.Key]);
                builder.Append("</a><h4>");
                builder.Append(pair.Value);
                builder.Append("</h4> <span id='");
                builder.Append(str);
                builder.Append("'>");
                builder.Append((float)((((float)pair.Value) / ((float)InputCount.AllCount)) * 100f));
                builder.Append("</span><script type='text/javascript'>$(\"#");
                builder.Append(str);
                builder.Append("\").progressBar({max:100,steps:1});</script></div>");
            }
            return builder.ToString();
        }

        private void LoadDic()
        {
            this.SubjectDic = Subject.GetDic();
            this.StatusDic = Status.GetDic();
        }

        private void StatisticsCount()
        {
            DateTime? startDate = null;
            if (!PageValidate.IsEmpty(this.StartDate.Text) && PageValidate.IsDateTime(this.StartDate.Text))
            {
                startDate = new DateTime?(DateTime.Parse(PageValidate.Form("StartDate")));
            }
            DateTime? endDate = null;
            if (!PageValidate.IsEmpty(this.EndDate.Text) && PageValidate.IsDateTime(this.EndDate.Text))
            {
                endDate = new DateTime?(DateTime.Parse(PageValidate.Form("EndDate")));
            }
            string[] source = CheckBoxListHelper.SelectedValueArr(this.SubjectList);
            IList<int> list = new List<int>();
            if ((source.Count<string>() > 0) && PageValidate.IsNumber(source.ToList<string>()))
            {
                list = Module_String.ToInt32(source);
            }
            source = CheckBoxListHelper.SelectedValueArr(this.StatusList);
            IList<int> list2 = new List<int>();
            if ((source.Count<string>() > 0) && PageValidate.IsNumber(source.ToList<string>()))
            {
                list2 = Module_String.ToInt32(source);
            }
            IList<QuestionStatusInfo> questionDic = Question.GetQuestionDic(startDate, endDate);
            if (questionDic.Count<QuestionStatusInfo>() == 0)
            {
                Utility.ShowBoxyAlert("找不到需要统计的数据", this);
            }
            else
            {
                IEnumerator<int> enumerator;
                StatisticsInfo inputCount = new StatisticsInfo();
                inputCount.AllCount = questionDic.Count<QuestionStatusInfo>();
                inputCount.Counts = new Dictionary<int, int>();
                using (enumerator = list2.GetEnumerator())
                {
                    Func<QuestionStatusInfo, bool> predicate = null;
                    int StatusId;
                    while (enumerator.MoveNext())
                    {
                        StatusId = enumerator.Current;
                        if (predicate == null)
                        {
                            predicate = delegate(QuestionStatusInfo a)
                            {
                                return a.StatusId == StatusId;
                            };
                        }
                        inputCount.Counts.Add(new KeyValuePair<int, int>(StatusId, questionDic.Where<QuestionStatusInfo>(predicate).Count<QuestionStatusInfo>()));
                    }
                }
                StringBuilder builder = new StringBuilder();
                builder.Append(this.CreateBlock("全局统计", inputCount, "0"));
                IList<QuestionStatusInfo> list4 = new List<QuestionStatusInfo>();
                using (enumerator = list.GetEnumerator())
                {
                    Func<QuestionStatusInfo, bool> func3 = null;
                    int SubjectId;
                    while (enumerator.MoveNext())
                    {
                        SubjectId = enumerator.Current;
                        if (func3 == null)
                        {
                            func3 = delegate(QuestionStatusInfo b)
                            {
                                return b.SubjectId == SubjectId;
                            };
                        }
                        list4 = questionDic.Where<QuestionStatusInfo>(func3).ToList<QuestionStatusInfo>();
                        inputCount = new StatisticsInfo();
                        inputCount.AllCount = list4.Count<QuestionStatusInfo>();
                        inputCount.Counts = new Dictionary<int, int>();
                        using (IEnumerator<int> enumerator2 = list2.GetEnumerator())
                        {
                            Func<QuestionStatusInfo, bool> func2 = null;
                            int StatusId;
                            while (enumerator2.MoveNext())
                            {
                                StatusId = enumerator2.Current;
                                if (func2 == null)
                                {
                                    func2 = delegate(QuestionStatusInfo c)
                                    {
                                        return c.StatusId == StatusId;
                                    };
                                }
                                inputCount.Counts.Add(new KeyValuePair<int, int>(StatusId, list4.Where<QuestionStatusInfo>(func2).Count<QuestionStatusInfo>()));
                            }
                        }
                        builder.Append(this.CreateBlock(this.SubjectDic[SubjectId], inputCount, SubjectId.ToString()));
                    }
                }
                this.AllCountLabel.Text = builder.ToString();
            }
        }
    }
}
